﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Symbonix.NorSoNet.Framework.BLL;
using System.Collections.Generic;
using Symbonix.NorSoNet.Framework;

public partial class Admin_Edit_Email_Templates : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        List<IEmailTemplate> emailTemplates = Symbonix.NorSoNet.Framework.BLL.ModuleManager.FrameworkFactory
            .GetEmailTemplate().GetAllTemplates();
        ddlEmailTemplates.DataTextField = "Name";
        ddlEmailTemplates.DataValueField = "Id";
        ddlEmailTemplates.DataSource = emailTemplates;
        ddlEmailTemplates.DataBind();
        ddlEmailTemplates.Items.Insert(0, new ListItem("Select Template","0"));
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            int id = int.Parse(ddlEmailTemplates.SelectedValue);
            if (id > 0)
            {
                IEmailTemplate template = Symbonix.NorSoNet.Framework.BLL.ModuleManager.FrameworkFactory.GetEmailTemplate().Load(id);
                if (template != null)
                {
                    template.Subject = tbxSubject.Text.Trim();
                    template.Body = fckBody.Value;
                    template.Update();
                }
            }
        }
    }

    protected void Template_Changed(object sender, EventArgs e)
    {
        int id = int.Parse(ddlEmailTemplates.SelectedValue);
        if (id > 0)
        {
            IEmailTemplate template = Symbonix.NorSoNet.Framework.BLL.ModuleManager.FrameworkFactory.GetEmailTemplate().Load(id);
            if (template != null)
            {
                tbxSubject.Text = template.Subject;
                fckBody.Value = template.Body;
            }
        }
    }
}
